package com.lnsoft.dqfs.client.test;

import java.nio.ByteBuffer;
import java.util.List;

import com.lnsoft.dqfs.server.util.MessageUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;


//	终端设备报文收发上下文
public class DeviceContext {
	

    /**
     * 当前为报文收发 主动站
     */
    private volatile int prm = 0;
    /**
     *  终端是否已完成初始化连接
     */
    private volatile boolean connectFlag = false;

    /**
     *  是否需要反转server端的fcb
     */
    private volatile boolean upServerFcb = false;


    private int readTimeOutNumber = 0;


    /**
     * 数据库对应终端主键ID
     */
    private volatile String clientId;


    private volatile int upgradePackageindex;

    /**
     *  server端Fcb
     */
    private volatile byte serverFcb = 0;

    /**
     *  客户端Fcb
     */
    private volatile byte clientFcb = 0;

    /**
     *  对应的是终端编号(0-65536)
     */
    private int clientName;

    //	终端报文的地址域两个字节
    private byte a1;
    private byte a2;

    /**
     *  服务端端口号
     */
    private int serverPort;

    private ByteBuf byteBuf = Unpooled.directBuffer(); // 堆外内存

    //  升级之前终端的版本号
    private String oldVersion = null;
    
    //	终端主发报文类型
    private int frameType = 0;
    
    //	当前已 上传遥测数据文件分组索引
    private int uploadIndex = 0;
    
    //	上传遥测数据文件分组
    private List<String> uploadFile;
    
    //	构造方法
    public DeviceContext(List<String> uploadFile) {
		super();
		this.uploadFile = uploadFile;
	}

	public List<String> getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(List<String> uploadFile) {
		this.uploadFile = uploadFile;
	}

	//	返回构造好的 上传分组 的字节数组
    public byte[] frameFactory() {
    	//	反转FCB
		setClientFcb((byte) (getClientFcb() ^ 1));
		//	创建终端请求帧的控制域
		byte clientC = MessageUtil.createClientC(getClientFcb(), (byte)1, (byte)3);
		//	上传遥测数据分组
        byte[] dataBytes = getUploadFile().get(getUploadIndex()).getBytes();
//        System.out.println("dataBytes.length:" + dataBytes.length);
        //	设置上传分组数据
        setUploadIndex(getUploadIndex() + 1);
        //	构造发送帧
        ByteBuffer buffer = ByteBuffer.allocate(1152).
                put(new byte[]{104, 0, 0, 104, clientC, getA1(), getA2(), (byte) 211, 1, 6,
                        getA1(), getA2(), 0, 0, 2, 9, 4, 0, 0, 0}).
                putInt(0);
        //  检查下发的 报文 是否是最后一个‘分组数据’,进行报文标识，便于终端进行识别
        if (getUploadIndex() == getUploadFile().size()) {
            buffer.put((byte) 0);
            setUploadIndex(-1);
        } else {
            buffer.put((byte) 1);
        }
        buffer.put(dataBytes);
        buffer.put((byte) MessageUtil.createCS(dataBytes, 0, dataBytes.length )).
                put((byte) 0).put((byte) 22);
		
        buffer.flip();
        byte[] bytes = new byte[buffer.limit()];
        buffer.get(bytes);
//        System.out.println("bytes.length:" + bytes.length);
        //	填充报文长度位
        bytes[1] = bytes[2] = (byte) (bytes.length - 6);
        bytes[bytes.length - 2] = (byte) MessageUtil.createCS(bytes, 4, bytes.length - 2);

        return bytes;
    }

	public int getUploadIndex() {
		return uploadIndex;
	}

	public void setUploadIndex(int uploadIndex) {
		this.uploadIndex = uploadIndex;
	}

	public int getFrameType() {
		return frameType;
	}

	public void setFrameType(int frameType) {
		this.frameType = frameType;
	}

	public int getPrm() {
		return prm;
	}

	public void setPrm(int prm) {
		this.prm = prm;
	}

	public boolean isConnectFlag() {
		return connectFlag;
	}

	public void setConnectFlag(boolean connectFlag) {
		this.connectFlag = connectFlag;
	}

	public boolean isUpServerFcb() {
		return upServerFcb;
	}

	public void setUpServerFcb(boolean upServerFcb) {
		this.upServerFcb = upServerFcb;
	}

	public int getReadTimeOutNumber() {
		return readTimeOutNumber;
	}

	public void setReadTimeOutNumber(int readTimeOutNumber) {
		this.readTimeOutNumber = readTimeOutNumber;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public int getUpgradePackageindex() {
		return upgradePackageindex;
	}

	public void setUpgradePackageindex(int upgradePackageindex) {
		this.upgradePackageindex = upgradePackageindex;
	}

	public byte getServerFcb() {
		return serverFcb;
	}

	public void setServerFcb(byte serverFcb) {
		this.serverFcb = serverFcb;
	}

	public byte getClientFcb() {
		return clientFcb;
	}

	public void setClientFcb(byte clientFcb) {
		this.clientFcb = clientFcb;
	}

	public int getClientName() {
		return clientName;
	}

	public void setClientName(int clientName) {
		this.clientName = clientName;
	}

	public byte getA1() {
		return a1;
	}

	public void setA1(byte a1) {
		this.a1 = a1;
	}

	public byte getA2() {
		return a2;
	}

	public void setA2(byte a2) {
		this.a2 = a2;
	}

	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	public ByteBuf getByteBuf() {
		return byteBuf;
	}

	public void setByteBuf(ByteBuf byteBuf) {
		this.byteBuf = byteBuf;
	}

	public String getOldVersion() {
		return oldVersion;
	}

	public void setOldVersion(String oldVersion) {
		this.oldVersion = oldVersion;
	}

    

}
